package com.boot.service.impl;

import ch.qos.logback.classic.spi.EventArgUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.DTO.SetmealDAO;
import com.boot.Vo.SetmealVo;
import com.boot.interceptor.BusinessException;
import com.boot.pojo.Setmeal;
import com.boot.pojo.SetmealDish;
import com.boot.result.Result;
import com.boot.result.ResultCodeNum;
import com.boot.service.SetmealDishService;
import com.boot.service.SetmealService;
import com.boot.mapper.SetmealMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author liyan
 * @description 针对表【setmeal(套餐)】的数据库操作Service实现
 * @createDate 2025-04-17 15:50:40
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishService setmealDishService;


    @Override
    public Result addSetmeal(SetmealDAO setmealDAO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDAO, setmeal, "setmealDishes");
        List<SetmealDish> setmealDishes = setmealDAO.getSetmealDishes();
        int insertRow = setmealMapper.insert(setmeal);
        if (insertRow == 0) {
            return Result.build(null, ResultCodeNum.INSERT_ERROR);
        }
        if (!setmealDishes.isEmpty()) {
            setmealDishes.forEach(sd -> sd.setSetmealId(setmeal.getId()));
            if (!setmealDishService.saveBatch(setmealDishes)) {
                throw new BusinessException("insert setmealDish failed", ResultCodeNum.SETEMEAL_INSERT_ERROR);
            }
        }
        return Result.success(setmeal);
    }

    @Override
    public Result getSetmealPage(SetmealVo setmealVo) {
        IPage<SetmealVo> page = new Page<>(setmealVo.getPage(), setmealVo.getPageSize());
        setmealMapper.selectMyPage(page, setmealVo);
        List<SetmealVo> list = page.getRecords();
        Long total = page.getTotal();
        return Result.success(Map.of("total", total, "records", list));
    }

    @Override
    public Result updateSetmealStatus(Long id, String status) {
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Setmeal::getId, id)
                .set(Setmeal::getStatus, status);
        int updateRow = setmealMapper.update(null, wrapper);
        if (updateRow == 0) {
            return Result.build(null, ResultCodeNum.UPDATE_ERROR);
        }
        return Result.success();
    }

    @Override
    public Result getSetmealById(Long id) {
        SetmealDAO data = setmealMapper.selectSetmealById(id);
        return Result.success(data);
    }

    @Override
    public Result updateSetMeal(SetmealDAO setmealDAO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDAO, setmeal, new String[]{"categoryName", "setmealDishes", "updateTime"});
        int updateRow = setmealMapper.updateById(setmeal);
        if (updateRow == 0) {
            return Result.build(null, ResultCodeNum.UPDATE_ERROR);
        }
        List<SetmealDish> setmealDishes = setmealDAO.getSetmealDishes();
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishService.remove(wrapper);
        if (!setmealDishes.isEmpty()) {
            setmealDishes.forEach(sd -> sd.setSetmealId(setmeal.getId()));
            if (!setmealDishService.saveBatch(setmealDishes)) {
                throw new BusinessException("insert setmealDish failed", ResultCodeNum.INSERT_ERROR);
            }
        }
        return Result.success();
    }

    @Override
    public Result deleteSetmeal(Long[] ids) {
        LambdaUpdateWrapper<Setmeal> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.in(Setmeal::getId, (Object[]) ids);
        int deleteRow = setmealMapper.delete(wrapper1);
        if (deleteRow == ids.length) {
            LambdaUpdateWrapper<SetmealDish> wrapper2 = new LambdaUpdateWrapper<>();
            wrapper2.in(SetmealDish::getSetmealId, (Object[]) ids);
            if (!setmealDishService.remove(wrapper2)) {
                throw new BusinessException("delete setmealDish failed", ResultCodeNum.DELETE_ERROR);
            }
            return Result.success();
        }
        return Result.build(null, ResultCodeNum.DELETE_ERROR);
    }
}




